package my.example.samplegridview;

import my.example.samplegridview.modal.review;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RatingBar;
import android.widget.Toast;

public class addreview extends Activity{
	Context mContext;
	addreview _self;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.addreview);
		
		mContext = this.getBaseContext();
		_self = this;
        Intent i = getIntent();
        
        final String product_id = i.getStringExtra("product_id");
        
        Button addreviewbackbutton = (Button)findViewById(R.id.addreviewback);
        addreviewbackbutton.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				finish();
			}
        	
        });
        
        Button addreviewbutton = (Button)findViewById(R.id.addreviewsubmit);
        addreviewbutton.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				EditText et = (EditText) _self.findViewById(R.id.addreviewauthorname);
				String authorname = et.getText().toString();
				
				if(authorname.trim().length() == 0){
						Toast.makeText(_self, "Author name cannot be empty!", Toast.LENGTH_SHORT).show();
						return;
				}
				
				et = (EditText) _self.findViewById(R.id.addreviewreviewtext);
				String reviewtext = et.getText().toString();
				
				if(authorname.trim().length() == 0){
					Toast.makeText(_self, "Review name cannot be empty!", Toast.LENGTH_SHORT).show();
					return;
				}
				
				RatingBar rb = (RatingBar) _self.findViewById(R.id.addreviewrating);
				float rating = rb.getRating();
				
				ContentValues cv = new ContentValues();
				//cv.put("review_id", "NULL");
				cv.put("product_id", product_id);
				cv.put("customer_id", "0");
				cv.put("author", authorname);
				cv.put("text", reviewtext);
				cv.put("rating", rating);
				
				Uri uri = _self.getContentResolver().insert(review.CONTENT_URI, cv);
				if(uri != null){
					Toast.makeText(_self, "Review successfully submitted.", Toast.LENGTH_SHORT).show();
					finish();
				}
				else{
					Toast.makeText(_self, "Review failed to submitted.", Toast.LENGTH_SHORT).show();
				}
			}
        	
        });
	}
}
